package com.alan.drools.demo.service;

import com.alan.drools.demo.model.Person;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.kie.api.KieBase;
import org.kie.api.runtime.KieSession;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class PersonService {

    public static List<Person> getlistPerson2(){
        KieBase kieBase = DroolsService.rulekieBase();
        List<Person> list= getlistPerson();
        for(Person person:list){
            KieSession kieSession= kieBase.newKieSession();
            kieSession.insert(person);
            int i=kieSession.fireAllRules();
            kieSession.dispose();
            log.info("i = {}, person:{}", i, JSON.toJSONString(person));
        }
        return list;
    }


    public static List<Person> getlistPerson(){
        //模拟从数据库中取数据
        List<Person> list= new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Person person = new Person();
            person.setAge(i + "0");
            person.setName(i + "test1");
            list.add(person);
        }
        Person person = new Person();
        person.setAge("22");
        person.setName("张三");
        list.add(person);
        return list;
    }

}
